/**
* @ProjectName: ShopSytem
* @Title: AdminGoodDaoImpl.java 
* @Package com.element5th.shop.dao.good 
* @Description: 后台商品管理相关操作
* @author 第五元素小组   
* @date 2012-10-26 15:06:16 
* @version V1.0   
*/ 
package com.element5th.shop.dao.good;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.element5th.shop.comm.BaseDao;
import com.element5th.shop.dto.BaseDto;
import com.element5th.shop.entity.DetailPic;
import com.element5th.shop.entity.GoodBigclass;
import com.element5th.shop.entity.GoodClass;
import com.element5th.shop.entity.ShopGood;

/**
 * The Class AdminGoodDaoImpl.
 */
public class AdminGoodDaoImpl extends BaseDao implements IAdminGoodDao {

	/*  
	* Title: addGood 
	* Description: 添加商品
	* @param dto 要添加的商品对象
	* @return 操作是否成功
	* @see com.element5th.shop.dao.good.IAdminGoodDao#addGood(com.element5th.shop.dto.BaseDto) 
	*/ 
	@Override
	public int addGood(BaseDto dto) {
		PreparedStatement ps = null;
		ResultSet rs = null;
		int goodId = 0;
		this.openConnection();
		String sql = "select SEQ_SHOP_GOOD.NEXTVAL from dual";
		Connection conn = this.getConnection();
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			if(rs.next())
				goodId = rs.getInt(1);
			this.close(ps, rs);
			dto.getShopGood().setGoodId(goodId);
			this.save(dto.getShopGood());
			this.commit();
			return goodId;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return goodId;
	}

	/*  
	* Title: updateGood 
	* Description: 更新商品
	* @param dto 要更新的商品对象
	* @return 操作是否成功
	* @see com.element5th.shop.dao.good.IAdminGoodDao#updateGood(com.element5th.shop.dto.BaseDto) 
	*/ 
	@Override
	public boolean updateGood(BaseDto dto) {
		// TODO Auto-generated method stub
		return false;
	}

	/*  
	* Title: deleteGood 
	* Description: 删除商品
	* @param dto 要删除的商品对象
	* @return 操作是否成功
	* @see com.element5th.shop.dao.good.IAdminGoodDao#deleteGood(com.element5th.shop.dto.BaseDto) 
	*/ 
	@Override
	public boolean deleteGood(BaseDto dto) {
		// TODO Auto-generated method stub
		return false;
	}

	/*  
	* Title: lowNumGood 
	* Description: 获得低库存商品列表
	* @return 低库存商品对象列表
	* @see com.element5th.shop.dao.good.IAdminGoodDao#lowNumGood() 
	*/ 
	@Override
	public List<BaseDto> lowNumGood() {
		// TODO Auto-generated method stub
		return null;
	}

	/*  
	* Title: setGoodOff 
	* Description: 设置商品打折
	* @param dto 打折信息对象
	* @return 操作是否成功
	* @see com.element5th.shop.dao.good.IAdminGoodDao#setGoodOff(com.element5th.shop.dto.BaseDto) 
	*/ 
	@Override
	public boolean setGoodOff(BaseDto dto) {
		// TODO Auto-generated method stub
		return false;
	}
	
	/*  
	* Title: setGoodOff 
	* Description: 查看商品打折
	* @return 商品打折信息列表
	* @see com.element5th.shop.dao.good.IAdminGoodDao#setGoodOff(com.element5th.shop.dto.BaseDto) 
	*/ 
	@Override
	public List<BaseDto> offList() {
		// TODO Auto-generated method stub
		return null;
	}
	
	/*  
	* Title: changeUserState 
	* Description: 更改打折状态
	* @param dto 打折信息对象
	* @param state 停止或者开始
	* @return 操作是否成功
	* @see com.element5th.shop.dao.user.IAdminUserDao#changeUserState(com.element5th.shop.dto.BaseDto, boolean) 
	*/ 
	@Override
	public boolean changeOffState(BaseDto dto, boolean state) {
		// TODO Auto-generated method stub
		return false;
	}

	/*  
	* Title: getBigClassList 
	* Description: 
	* @return 
	* @see com.element5th.shop.dao.good.IAdminGoodDao#getBigClassList() 
	*/ 
	@Override
	public List<BaseDto> getBigClassList() {
		List<BaseDto> list = new ArrayList<BaseDto>();
		this.openConnection();
		List<GoodBigclass> bigClassList = this.findAll(GoodBigclass.class);
		for (GoodBigclass goodBigclass : bigClassList) {
			BaseDto dto = new BaseDto();
			dto.setGoodBigClass(goodBigclass);
			list.add(dto);
		}
		this.commit();
		return list;
	}

	/*  
	* Title: addBigClass 
	* Description: 
	* @param dto
	* @return 
	* @see com.element5th.shop.dao.good.IAdminGoodDao#addBigClass(com.element5th.shop.dto.BaseDto) 
	*/ 
	@Override
	public boolean addClass(BaseDto dto) {
		this.openConnection();
		boolean result = this.save(dto.getGoodClass());
		this.commit();
		return result;
	}

	/*  
	* Title: addBigClass 
	* Description: 
	* @param dto
	* @return 
	* @see com.element5th.shop.dao.good.IAdminGoodDao#addBigClass(com.element5th.shop.dto.BaseDto) 
	*/ 
	@Override
	public boolean addBigClass(BaseDto dto) {
		this.openConnection();
		boolean result = this.save(dto.getGoodBigClass());
		this.commit();
		return result;
	}

	/*  
	* Title: getClassList 
	* Description: 
	* @param bigClassId
	* @return 
	* @see com.element5th.shop.dao.good.IAdminGoodDao#getClassList(int) 
	*/ 
	@Override
	public List<BaseDto> getClassList(int bigClassId) {
		List<BaseDto> list = new ArrayList<BaseDto>();
		this.openConnection();
		List<GoodClass> classList = this.findListByCol(GoodClass.class, "bigclassId", bigClassId);
		for (GoodClass goodBigclass : classList) {
			BaseDto dto = new BaseDto();
			dto.setGoodClass(goodBigclass);
			list.add(dto);
		}
		this.commit();
		return list;
	}

	/*  
	* Title: getGoodList 
	* Description: 
	* @return 
	* @see com.element5th.shop.dao.good.IAdminGoodDao#getGoodList() 
	*/ 
	@Override
	public List<BaseDto> getGoodList() {
		List<BaseDto> list = new ArrayList<BaseDto>();
		List<ShopGood> goodList = new ArrayList<ShopGood>();
		this.openConnection();

		goodList = this.findAll(ShopGood.class);

		for (ShopGood shopGood : goodList) {
			BaseDto dto = new BaseDto();
			dto.setShopGood(shopGood);
			list.add(dto);
		}
		this.commit();
		return list;
	}

	/*  
	* Title: addGoodDeatail 
	* Description: 
	* @param dto
	* @return 
	* @see com.element5th.shop.dao.good.IAdminGoodDao#addGoodDeatail(com.element5th.shop.dto.BaseDto) 
	*/ 
	@Override
	public boolean addGoodDeatail(BaseDto dto) {
		PreparedStatement ps = null;
		ResultSet rs = null;
		int detailId = 0;
		this.openConnection();
		Connection conn = this.getConnection();
		String sql = "select SEQ_SHOP_GOOD.NEXTVAL from dual";
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			if(rs.next())
				detailId = rs.getInt(1);
			this.close(ps, rs);
			dto.getGoodDetail().setDetailId(detailId);
			this.save(dto.getGoodDetail());
			for (DetailPic pic : dto.getGoodDetail().getDetailPics()) {
				pic.setDetailId(detailId);
				this.save(pic);
			}
			this.commit();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
}
